package com.yowasa.xiuxian.engine.message.activity;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.UserTagHelper;
import com.yowasa.xiuxian.util.ComponentUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


@CMD(name = "/身份组领取")
public class MemberRoleHandler extends CmdMessageHandler {
    public static final Logger logger = LoggerFactory.getLogger(MemberRoleHandler.class);
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private UserTagHelper userTagHelper;



    @Override
    public void execute(SendMessage message) {
        String memberRoles = messageHelper.getMemberRoles(message);
        logger.info("获取结果：{}",memberRoles);

        if(Integer.parseInt(message.getVilla_id())!=2361){
            throw new HandlerException("仅在汐颜bot开发者大别野可使用该指令领取身份组,若别野房东有意向该指令可私聊作者");
        }

        List<String> params = getAllParam(message);
        ArrayList<String> list = Lists.newArrayList("仙风道骨", "富可敌国", "先天道体", "九转轮回");
        if (StringUtils.isEmpty(params.get(0))) {
            List<List<MysComponent>> componentList = ComponentUtil.getComponentList("/身份组领取 ",1,list);
            messageHelper.replyMysPanel(message, "请追加你要领取的身份组名称，当前可领取的有  仙风道骨, 富可敌国, 先天道体, 九转轮回",null,componentList,null);
            return;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid",Integer.parseInt(message.getFrom_user_id()));
        jsonObject.put("is_add",true);

        UserBO userBO = userHelper.checkOpUser(message);
        String s = params.get(0);
        int i = list.indexOf(s);
        boolean flag = false;
        String msg ="";
        switch (i){
            case 0:
                if(userBO.getUserConfig().getAbility() >= 100){
                    jsonObject.put("role_id",99394);
                    flag =true;
                }else {
                    msg = "仙风道骨需要道行大于100，你当前仅有"+userBO.getUserConfig().getAbility();
                }
                break;
            case 1:
                if(userBO.getUserConfig().getGold() >= 1000000){
                    jsonObject.put("role_id",98948);
                    flag =true;
                }else {
                    msg = "富可敌国需要摩拉大于100w，你当前仅有"+userBO.getUserConfig().getGold();
                }
                break;
            case 2:
                if(userBO.getUserAttr().getLuc() == 100
                        && userBO.getUserAttr().getInts() == 100
                        && userBO.getUserAttr().getUnd() == 100
                        && userBO.getUserAttr().getCon() == 100
                        && userBO.getUserAttr().getDex() == 100
                        && userBO.getUserAttr().getStr() == 100 ){
                    jsonObject.put("role_id",99395);
                    flag =true;
                }else {
                    msg = "先天道体需要各项基础属性均为100，您当前不满足条件";
                }
                break;
            case 3:
                int value = userTagHelper.getValue(userBO.getUserId(), XiuxianConstants.TAG_FEISHENG);
                logger.info("获取当前飞升tag：----  {}",value);
                if(value >= 9){
                    jsonObject.put("role_id",99396);
                    flag =true;
                }else {
                    msg = "九转轮回需要完成至少九次转世，您当前转世次数为"+value;
                }
                break;
            default:
                messageHelper.replyMys(message,"没有这样的身份组");
                return;
        }
        if(flag){
            messageHelper.operateMemberToRole(message,jsonObject);
            messageHelper.replyMys(message,"恭喜你，领取成功");
        }else {
            messageHelper.replyMys(message,msg);
        }

    }


    public String desc() {
        return "深渊挑战";
    }


}
